package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService mealService;
    @Override
    public R<String> remove(Long id) throws CustomException {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> mealWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        mealWrapper.eq(Setmeal::getCategoryId, id);
        int dishCount = dishService.count(dishWrapper);
        int mealCount = mealService.count(mealWrapper);
        if (dishCount > 0) {
//            return R.error("莹莹温馨提示：下面有东西删不了哦");
            throw new CustomException("莹莹温馨提示：下面有东西删不了哦");
        }
        if (mealCount > 0) {
//            return R.error("莹莹温馨提示：下面有东西删不了哦");
            throw new CustomException("莹莹温馨提示：下面有东西删不了哦");
        }
        removeById(id);
        return R.success("莹莹告诉你：删除成功哦");
    }
}
